package edu.olin.scheduler.common;

/**
 * Represents a simple 24-hour time-of-day without any timezone data.
 * SimpleTimes are immutable.
 */
public class SimpleTime implements Comparable {
    
    private int hours; //24-hour hours, local time
    private int minutes; //number of minutes past the hour
    
    /**
     * Accessor method.
     * @return The minutes component of this SimpleTime
     */
    public int minutes(){ return minutes; }
    
    /**
     * Accessor method.
     * @return The minutes component of this SimpleTime
     */
    public int hours(){ return hours; }
    
    /**
     * Gives the number of minutes since midnight that this SimpleTime represents
     * minutesSinceMidnight() simply returns
     *    <CODE>minutes()+60*hours()</CODE>
     * @return The number of minutes since midnight for this SimpleTime
     */
    public int minutesSinceMidnight(){
        return hours*60 + minutes;
    }
    
    /**
     * Creates a new SimpleTime as specified
     * @param hours The 24-hour hour component of this SimpleTime.
     * 0 <= hours < 24
     * @param minutes The minute component of this SimpleTime.
     * 0 <= minutes < 60
     */
    public SimpleTime(int hours, int minutes){
        if(hours<0||hours>=24){ throw new IllegalArgumentException("Invalid hours.  0 <= hours < 24 "); }
        if(minutes<0||minutes>=60){ throw new IllegalArgumentException("Invalid minutes.  0 <= minutes < 60 "); }
        this.hours = hours;
        this.minutes = minutes;
    }
    
    /**
     * Determines if two SimpleTimes are equivalent.
     * @param other Another SimpleTime to test for equality with this one
     * @return true if both SimpleTimes have the same hour and minute; false otherwise
     */
    public boolean equals(Object other){
        SimpleTime st = (SimpleTime) other;
        return st.hours==hours && st.minutes==minutes;
    }
    
    /**
     * Compares two SimpleTimes for sorting.
     * @param other The other SimpleTime to compare to this one
     * @return m is the difference, in minutes, between the two times.
     * -m if this SimpleTime is earlier in the day than <B>other</B>
     * 0 if the two SimpleTimes are equivalent
     * +m if this SimpleTime is later in the day than <B>other</B>
     */
    public int compareTo(Object other){
        SimpleTime st = (SimpleTime) other;
        return minutesSinceMidnight() - st.minutesSinceMidnight();
    }
    
    public boolean isBefore(SimpleTime other){
        return this.compareTo(other)<0;
        }
    public boolean isAfter(SimpleTime other){
        return this.compareTo(other)>0;
        }
    
    //print out this SimpleTime in AM/PM format
    public String toString(){ return toString(false); }
    
    //print out this SimpleTime in the specified format
    public String toString(boolean is24hour){
        int h = (is24hour ? hours : hours%12);
        if(!is24hour && h==0){ h=12; }
        int m = minutes;
        String ampm = (is24hour ? "" : (hours<12?" AM":" PM") );
        return h+":"+(m<10?"0":"")+m + ampm;
    }
    
}
